package utils
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import framework.dao.EntryUtil;
	
	import isoengine.IsoSetting;
	import isoengine.values.MapDataList;
	import isoengine.values.MapSkillRangeList;
	import isoengine.values.MapSpriteList;
	import isoengine.values.MapTileList;

	public class XmlLoader extends EventDispatcher
	{
		private static var instance:XmlLoader
		
		private var _xml:XML
		
		private var _loader:URLLoader	//读地图数据		
		
		public var xmlList:Array = [];
		
		private var _url:String;
		
		public function XmlLoader(single:Private)
		{
			if (single==null) {
				throw new Error("单例模式！无需生成实例，请直接静态使用！");
			}
		}
		
		public static function getInstance():XmlLoader{
			if(instance == null){
				instance = new XmlLoader(new Private)
			}
			return instance
		}
		
		public var sucFun:Function = null;
		
		public function loadMapXML():void{					
			_loader = new URLLoader();
			_url = xmlList.shift();
			_loader.load(new URLRequest(_url))	;		
			_loader.addEventListener(Event.COMPLETE , loadConfigCompleteHandel);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, loadErrorHandel);			
		}
		
		public static var configXML:XML;
		public static var spriteXML:XML;
		public static var mapXML:XML;
		
		private function loadConfigCompleteHandel(evt:Event):void{			
			dispose();			
			//数据用来给 DataCenter 中的 SettingData赋值
			var xmlData:XML = new XML(_loader.data);
				
			switch(_url)
			{
				case "../mock_data/map/map_config.xml":	
					configXML = xmlData;					
					break;				
				
				case "../mock_data/map/debug_sprite.xml":
					spriteXML = xmlData;					
					break;				
				
				case "../mock_data/map/m1.xml":
					mapXML = xmlData;					
					break;
				
				default:
					//
					
			}
			
			if(xmlList.length == 0)
			{				
				sucFun();
			}
			else
			{
				//继续
				loadMapXML();
			}
			
		}
		
		private function loadErrorHandel(event:Event):void{
			dispose();			
			trace("load xml file error! -> " + event.target)
		}
		
		private function dispose():void
		{
			_loader.removeEventListener(Event.COMPLETE , loadConfigCompleteHandel);			
			_loader.removeEventListener(IOErrorEvent.IO_ERROR, loadErrorHandel);		
		}
		
	}
}

class Private{}